const Koa = require('koa');

const app = new Koa();

const router = require('koa-router')();
const body = require('koa-bodyparser');
const cors = require('koa2-cors');
const jwtKoa = require('koa-jwt');
const fs = require('fs');
const https = require('https');
const enforceHttps = require('koa-sslify');
const views = require('koa-views');
const path = require('path');
const secret = require('./config/secret.json');
// 中间件
const vt = require('./middleware/verifyToken');
const err = require('./middleware/error');
const { logger, accessLogger } = require('./middleware/logger');
// 注册路由
const allRouter = require('./router');


const options = {
  key: fs.readFileSync('./ssl/2_www.bbtjym.com.key'),
  cert: fs.readFileSync('./ssl/1_www.bbtjym.com_bundle.crt'),
};

app.use(body());
app.use(cors());
// router.use('/login', login);
app.use(err.error);
app.use(
  jwtKoa({ secret: secret.sign }).unless({
    path: [/^\/login\/*/, /^\/reginster/, /^\/url\/*/, /^\/resume\/*/],
  }),
);
allRouter(app);
app.use(accessLogger());
app.use(views(path.join(__dirname, './view'), {
  extension: 'ejs',
}));
app.use(vt.jwtToken);
// router.use('/user', user);
app.use(err.error);
app.use(router.routes()).use(router.allowedMethods());
// https.createServer(options, app.callback()).listen(7101); // 正式库
https.createServer(options, app.callback()).listen(7100); //  测eeeea试库
